/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatclient.applets.components;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JTextField;

/**
 *
 * @author ADZongo
 */
public class FancyTextField extends JTextField implements FocusListener {
    private Shape shape;
    private boolean onFocus;
    
    public FancyTextField() {
        super();
        setOpaque(false); // As suggested by @AVD in comment.
        this.addFocusListener(this);
        onFocus = false;
    }
    
    public FancyTextField(int size) {
        super(size);
        setOpaque(false); // As suggested by @AVD in comment.
        this.addFocusListener(this);
        onFocus = false;
    }
    
    /**
     * Paints the text field.
     * @param g 
     */
    protected void paintComponent(Graphics g) {
         g.setColor(getBackground());
         g.fillRoundRect(0, 0, getWidth()-1, getHeight()-1, 0, 0);
         super.paintComponent(g);
    }
    
    /**
     * Paints the text field borders.
     * @param g 
     */
    protected void paintBorder(Graphics g) {
         g.setColor(onFocus ? Color.LIGHT_GRAY : Color.BLACK/*getForeground()*/);
         g.drawRoundRect(0, 0, getWidth()-1, getHeight()-1, 0, 0);
    }
    
    
    public boolean contains(int x, int y) {
         if (shape == null || !shape.getBounds().equals(getBounds())) {
             shape = new RoundRectangle2D.Float(0, 0, getWidth()-1, getHeight()-1, 1, 1);
         }
         return shape.contains(x, y);
    }
    
    /**
     * Focus received.
     * @param e 
     */
    public void focusGained(FocusEvent e) {
        onFocus = true;
        repaint();
    }

    /**
     * Focus gone.
     * @param e 
     */
    public void focusLost(FocusEvent e) {
        onFocus = false;
        repaint();
    }
}
